package com.vector.update_app.view;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;

import androidx.appcompat.app.AppCompatActivity;

import com.github.ybq.android.spinkit.dialog.MySpinKitDialog;
import com.vector.update_app.debug.App;

/**
 * Created by Vector on 2016/8/12 0012.
 */
public class CProgressDialogUtils {
    private static final String TAG = CProgressDialogUtils.class.getSimpleName();
    //    private static ProgressDialog sCircleProgressDialog;
    private static MySpinKitDialog mSpinKitDialog;

    private CProgressDialogUtils() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    public static void showProgressDialog(AppCompatActivity activity) {
        showProgressDialog(activity, "加载中", false, null);
    }

    public static void showProgressDialog(AppCompatActivity activity, DialogInterface.OnCancelListener listener) {
        showProgressDialog(activity, "加载中", true, listener);
    }

    public static void showProgressDialog(AppCompatActivity activity, String msg) {
        showProgressDialog(activity, msg, false, null);
    }

    public static void showProgressDialog(AppCompatActivity activity, String msg, DialogInterface.OnCancelListener listener) {
        showProgressDialog(activity, msg, true, listener);
    }

    public static void showProgressDialog(final AppCompatActivity activity, String msg, boolean cancelable, DialogInterface.OnCancelListener listener) {
        if (activity == null || activity.isFinishing()) {
            return;
        }

        if (mSpinKitDialog == null) {
            mSpinKitDialog = new MySpinKitDialog();
            mSpinKitDialog.show(activity.getSupportFragmentManager(), "update_app_dialog");
        } else {
            if (activity.equals(mSpinKitDialog.getDialog().getOwnerActivity())) {
                mSpinKitDialog = new MySpinKitDialog();
                mSpinKitDialog.getDialog().setOwnerActivity(activity);
                mSpinKitDialog.getDialog().setOnCancelListener(listener);
                mSpinKitDialog.setCancelable(cancelable);
            } else {
                //不相等,所以取消任何ProgressDialog
                cancelProgressDialog();
                mSpinKitDialog = new MySpinKitDialog();
                mSpinKitDialog.setCancelable(cancelable);
                mSpinKitDialog.getDialog().setOwnerActivity(activity);
                mSpinKitDialog.getDialog().setOnCancelListener(listener);
            }
        }
        /*if (sCircleProgressDialog == null) {
            sCircleProgressDialog = new ProgressDialog(activity);
            sCircleProgressDialog.setMessage(msg);
            sCircleProgressDialog.setOwnerActivity(activity);
            sCircleProgressDialog.setOnCancelListener(listener);
            sCircleProgressDialog.setCancelable(cancelable);
        } else {
            if (activity.equals(sCircleProgressDialog.getOwnerActivity())) {
                sCircleProgressDialog.setMessage(msg);
                sCircleProgressDialog.setCancelable(cancelable);
                sCircleProgressDialog.setOnCancelListener(listener);
            } else {
                //不相等,所以取消任何ProgressDialog
                cancelProgressDialog();
                sCircleProgressDialog = new ProgressDialog(activity);
                sCircleProgressDialog.setMessage(msg);
                sCircleProgressDialog.setCancelable(cancelable);
                sCircleProgressDialog.setOwnerActivity(activity);
                sCircleProgressDialog.setOnCancelListener(listener);
            }
        }

        if (!sCircleProgressDialog.isShowing()) {
            sCircleProgressDialog.show();
        }*/

    }


    public static void cancelProgressDialog(Activity activity) {
        if (mSpinKitDialog != null && mSpinKitDialog.getDialog() != null & mSpinKitDialog.getDialog().isShowing()) {
            mSpinKitDialog.dismiss();
            mSpinKitDialog = null;
        }
        /*if (sCircleProgressDialog != null && sCircleProgressDialog.isShowing()) {
            if (sCircleProgressDialog.getOwnerActivity() == activity) {
                sCircleProgressDialog.cancel();
                sCircleProgressDialog = null;
            }
        }*/
    }

    public static void cancelProgressDialog() {
        if (mSpinKitDialog != null && mSpinKitDialog.getDialog().isShowing()) {
            mSpinKitDialog.getDialog().cancel();
            mSpinKitDialog = null;
        }
        /*if (sCircleProgressDialog != null && sCircleProgressDialog.isShowing()) {
            sCircleProgressDialog.cancel();
            sCircleProgressDialog = null;
        }*/
    }

}
